package it.fuesi.myseries.activities;

import it.fuesi.myseries.R;
import it.fuesi.myseries.api.ApiBrokerService;
import it.fuesi.myseries.api.ApiMethod;
import it.fuesi.myseries.model.MySeriesConstant;
import it.fuesi.myseries.model.Serie;
import it.fuesi.myseries.model.adapters.SerieItemAdapter;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class SearchResultsActivity extends Activity implements Observer {

	private ArrayList<Serie> searchResults;
	private SerieItemAdapter searchResultsAdapter;
	
	private ListView listResults;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_results);
		
		listResults = (ListView) findViewById(R.id.listResults);
		listResults.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				openDetail(searchResults.get(index));
			}
		});
		searchResults = new ArrayList<Serie>();
		searchResultsAdapter = new SerieItemAdapter(this,
				R.layout.serie_list_item, searchResults);

		listResults.setAdapter(searchResultsAdapter);
		
		// Get the intent, verify the action and get the query
	    Intent intent = getIntent();
	    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
	      String query = intent.getStringExtra(SearchManager.QUERY);
	      searchSerie(query);
	    }
		
		
	}
	
	public void closeSearchResults(MenuItem item){
		this.finish();
	}

	public void openDetail(Serie serie) {
		Intent intent = new Intent(this, SerieDetailActivity.class);
		intent.putExtra("EXTRA_MESSAGE", serie);
		startActivity(intent);
	}
	
	@Override
	public boolean onSearchRequested() {
		System.out.println("Search requested!");
		return super.onSearchRequested();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search_results, menu);
		return true;
	}

	
	public void searchSerie(String query) {

		ProgressBar p = new ProgressBar(listResults.getContext());
		p.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, Gravity.CENTER));
		p.setIndeterminate(true);

		listResults.setEmptyView(p);


		Intent intent = ApiBrokerService.getApiTaskIntent(this, ApiMethod.searchSerie);
		
		intent.putExtra(MySeriesConstant.SEARCH.toString(), query);

		startService(intent);

	}

	@Override
	public void update(Observable observable, Object results) {
		searchResultsAdapter.clear();
		
		final Bundle bundle = (Bundle) results;
		
		if(bundle.getBoolean(MySeriesConstant.SUCCESS.toString())){
		
			final Bundle data = bundle.getBundle(MySeriesConstant.DATA.toString());
			
			for (String key : data.keySet()) {
				final Serie s = (Serie) data.getSerializable(key);
				searchResultsAdapter.add(s);
			}
		}else{
			String msg = "Search failed! " + bundle.getString(MySeriesConstant.ERROR.toString());
			Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
		}
			

	}

}
